<template>
  <div class="sku" @click="hide">
    <div class="goods-sku" @click="fn($event)">
      <div class="card">
        <div class="x" @click="hide">x</div>

        <van-card
          :num="cSku.itemNum"
          :price="cSku.minPrice"
          :desc="cSku.recommendStatusStr"
          :title="cSku.name"
          :thumb="cSku.pic"
        >
          <div slot="footer">
            <div class="step">
              <p class="title">购买数量</p>
              <van-stepper v-model="cSku.itemNum" />
            </div>
            <div class="actions">
              <van-button  size="large" type="warning" id="subBtn" @click="handleClick">{{ btnText }}</van-button>
            </div>
          </div>
        </van-card>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    sku: {
      type: Object,
      required: true
    },
    actionButton: {
      type: Number,
      default: 0 // 0 代表加入购物车 1代表立即购买
    }
  },
  methods: {
    handleClick() {
      // 点击提交按钮  判断 点击的是 加入购物车还是立即购买
      this.cSku.isChecked = true;
      if (this.actionButton == 1) {
        this.$emit("buyNow", this.cSku);
      } else {
        this.$emit("addCart", this.cSku);
      }
    },
    hide() {
      this.$emit("skuHide");
    },
    fn(e) {
      e.stopPropagation();
    }
  },
  computed: {
    cSku() {
      let cSku = JSON.parse(JSON.stringify(this.sku));
      cSku.itemNum = 1;
      return cSku;
    },
    btnText() {
      if (this.actionButton === 0) {
        return "加入购物车";
      } else {
        return "立即购买";
      }
    }
  }
};
</script>

<style lang="less" scoped>
.sku {
  position: fixed;
  z-index: 200;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  .goods-sku {
    position: absolute;
    background: #fff;
    box-sizing: border-box;
    width: 100%;
    left: 0;
    bottom: 0;
    padding-top: 10px;
    border-radius: 10px 10px 0 0;
    .card {
      position: relative;
      .x {
        text-align: center;
        line-height: 20px;
        z-index: 100;
        color: #aaa;
        font-size: 18px;
        position: absolute;
        right: 9px;
        top: -13px;
        width: 20px;
        height: 20px;
        background: #fff;
      }
    }
    .van-card {
      padding: 0;
      padding-top: 30px;
      &__header {
        padding: 0 10px;
      }
    }
  }
  .step {
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 10px;
  }
  .van-button--large {
    background-image: linear-gradient(90deg, #ff971d 0, #fdb35c 100%),
      linear-gradient(#ccc, #ccc);
    margin: 0;
    margin-top: 10px;
  }
}
</style>